package com.sjh.tomcat;

import com.sjh.tomcat.handler.SjhRequestHandler;
import com.sjh.tomcat.servlet.SjhHttpServlet;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.xml.sax.SAXException;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 通过 xml + 反射初始化容器
 */
public class SjhTomcatV3 {

    public static final ConcurrentHashMap<String, SjhHttpServlet> servletMapping = new ConcurrentHashMap<>();

    public static final ConcurrentHashMap<String, String> servletUrlMapping = new ConcurrentHashMap<>();

    public SjhTomcatV3(){

    }

    public static void main(String[] args) {
        SjhTomcatV3 sjhTomcatV3 = new SjhTomcatV3();
        sjhTomcatV3.init();
        sjhTomcatV3.run();
    }

    public void run(){
        try {
            ServerSocket serverSocket = new ServerSocket(8080);
            System.out.println("=======SjhTomcatV3在8080端口监听");
            while(!serverSocket.isClosed()){
                Socket socket = serverSocket.accept();
                new Thread(new SjhRequestHandler(socket)).start();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void init(){

        // 得到web.xml文件的路径
        String path = SjhTomcatV3.class.getResource("/").getPath();
        System.out.println("path = " + path);
        // 使用 dom4j技术完成读取
        SAXReader saxReader = new SAXReader();
        try {
            saxReader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
        } catch (SAXException e) {
            e.printStackTrace();
        }
        Document document = null;
        try {
            document = saxReader.read(new File(path + "web.xml"));
            Element rootElement = document.getRootElement();
            //得到根元素下的所有元素
            List<Element> elements = rootElement.elements();
            //遍历并过滤到 servlet sevlet-mapping
            /**
             * <servlet>
             *         <servlet-name>SjhCalServlet</servlet-name>
             *         <servlet-class>com.sjh.tomcat.servlet.SjhCalServlet</servlet-class>
             *     </servlet>
             *     <servlet-mapping>
             *         <servlet-name>SjhCalServlet</servlet-name>
             *         <url-pattern>/SjhCalServlet</url-pattern>
             *     </servlet-mapping>
             */
            for(Element element : elements){
                if("servlet".equalsIgnoreCase(element.getName())){
                    //使用反射，将servlet实例放入容器中
                    Element servletName = element.element("servlet-name");
                    Element servletClass = element.element("servlet-class");
                    servletMapping.put(servletName.getText(), (SjhHttpServlet) Class.forName(servletClass.getText()).newInstance());
                } else if ("servlet-mapping".equalsIgnoreCase(element.getName())) {
                    Element servletName = element.element("servlet-name");
                    Element servletUrl = element.element("url-pattern");
                    servletUrlMapping.put(servletUrl.getText(), servletName.getText());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
